﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.WebUtilities;

namespace DataWorks_WebApi.Tools
{
    public class AbsoluteRedirectUri
    {
        public static RedirectContext<CookieAuthenticationOptions> RebuildRedirectUri(
    RedirectContext<CookieAuthenticationOptions> context)
        {
            const string ACCOUNT_SITE = "http://localhost:3000/ims/noauth"; // 替换为实际的账户站点URL

            if (context.RedirectUri.StartsWith(ACCOUNT_SITE))
                return context;

            var originUri = new Uri(context.RedirectUri);
            var uriBuilder = new UriBuilder(ACCOUNT_SITE);
            uriBuilder.Path = originUri.AbsolutePath;
            //var queryStrings = QueryHelpers.ParseQuery(originUri.Query);
            //var returnUrlName = context.Options.ReturnUrlParameter;
            //var returnUrl = originUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped) + queryStrings[returnUrlName];
            //uriBuilder.Query = QueryString.Create(returnUrlName, returnUrl).ToString();
            context.RedirectUri = uriBuilder.ToString();
            return context;
        }
    }
}
